package shop_service

import (
	"errors"
	"myserver/app/model"
	"myserver/app/types"
	"myserver/pkg/utils"
)

type ShopService struct {
	List []types.UserShop
}

func (this_shop ShopService) CeateShop() error {
	l := len(this_shop.List)
	if l == 0 {
		return errors.New("创建的数据为空")
	}
	for i := 0; i < l; i++ {
		this_shop.List[i].Uuid = utils.GetUuid()
	}
	err := model.AddShop(&this_shop.List)
	if err != nil {
		return err
	} else {
		return nil
	}
}
func (this_shop ShopService) UpDataShop() error {
	var (
		uuids []string
	)
	for i := 0; i < len(this_shop.List); i++ {
		uuids = append(uuids, this_shop.List[i].Uuid)
	}
	datas, err := model.FindShopByUUids(uuids...)
	for i := 0; i < len(datas); i++ {
		for j := 0; j < len(this_shop.List); j++ {
			if this_shop.List[j].Uuid == datas[i].Uuid {
				datas[i].ShopName = this_shop.List[j].ShopName
				datas[i].ShopAddress = this_shop.List[j].ShopAddress
				datas[i].ShopPictureKey = this_shop.List[j].ShopPictureKey
				datas[i].ShopInfo = this_shop.List[j].ShopInfo
			}
		}
	}
	if err != nil {
		return err
	}
	err = model.UpDataShop(&datas)
	if err != nil {
		return err
	} else {
		return nil
	}
}

func (ShopService) ListData(page int, size int, key string) ([]types.UserShop, int64, error) {
	return model.GETList[types.UserShop](page, size, "shop_name", key)
}
